#include "fftclass.h"

FFTClass::FFTClass() {}

QList<QList<QList<int> > > FFTClass::initData(int fftdos, int num_chirps, int rx_channels)
{

    QList<QList<QList<int>>> fftdata; // channels-chirps-fftdos
    int val;

    for(int ch=0; ch<rx_channels; ch++) {
        QList<QList<int>> channeldata;
        for(int chirp=0; chirp<num_chirps; chirp++) {
            val = (ch - 4) * 500  +  chirp * 1;
            QList<int> chirpdaa;
            for(int d=0; d<fftdos; d++) {
                chirpdaa.append(val++);
            }
            channeldata.append(chirpdaa);
        }

        fftdata.append(channeldata);
    }
    return fftdata;
}


void FFTClass::setupFFTDemo(QCustomPlot *customPlot, QList<QList<int>> channeldata )
{
    // qDebug() << "FFTClass::setupSimpleItemDemo" << channeldata.length() << channeldata.at(0).length();
    if (channeldata.length() == 0 || channeldata.at(0).length() == 0) return;

    QString demoName = "Simple Demo";
    QPen pen;

    int chirp_num = channeldata.size();
    int fftdots = channeldata.at(0).size();
    // qDebug() << "FFTClass::setupSimpleItemDemo" << "chirp_num" << (chirp_num);
    // qDebug() << "FFTClass::setupSimpleItemDemo" << "fftdots" << (fftdots);

    customPlot->setOpenGl(false);
    for (int i=0; i<chirp_num; i++) {
        pen.setColor(QColor(qSin(i*0.3)*100+100, qSin(i*0.6+0.7)*100+100, qSin(i*0.4+0.6)*100+100));
        customPlot->addGraph();
        customPlot->graph(i)->setPen(pen); // line color blue for first graph
    }

    QVector<double> x(fftdots), y(fftdots);
    for (int i=0; i<fftdots; i++)
    {
        x[i] = i;
    }

    customPlot->xAxis2->setVisible(false);
    customPlot->xAxis2->setTickLabels(false);

    customPlot->xAxis->setRangeLower(0);
    customPlot->xAxis->setRangeUpper(fftdots);
    customPlot->xAxis->setSubTicks(false);
    customPlot->xAxis->setTickLabels(true);


    customPlot->yAxis2->setVisible(false);
    customPlot->yAxis2->setTickLabels(false);
    customPlot->yAxis->setSubTicks(false);

    double minvalue = 0xffffffff, maxvalue = 0 - minvalue;
    for (int chirp=0; chirp<chirp_num; chirp++)
    {
        auto chirpdata = channeldata[chirp];
        for (int i=0; i<fftdots; i++) {
            y[i] = chirpdata[i];
            // if (i<12 && chirp < 5) printf("%d ", (int)y[i]);
            minvalue = std::min<double>(minvalue, y[i]);
            maxvalue = std::max<double>(maxvalue, y[i]);
        }
        // if (chirp < 5) printf("\n");
        customPlot->graph(chirp)->setData(x, y);
        // qDebug() << "FFTClass::setupSimpleItemDemo" << y[0] << y[1] << "-" << y[fftdots-2] << y[fftdots-1];
        if (chirp == 0)
            // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
            customPlot->graph(0)->rescaleAxes();
        else
            // same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
            customPlot->graph(chirp)->rescaleAxes(true);
    }

    // customPlot->yAxis->setRangeLower(minvalue);
    // customPlot->yAxis->setRangeUpper(maxvalue);
    qDebug() << "FFTClass::setupSimpleItemDemo" << customPlot << "yAxis.range" << (minvalue) << "-" << maxvalue;

    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}
